package com.yanqu.road.entity.activity;

import com.yanqu.road.entity.DataObject;
import com.yanqu.road.entity.FieldTypeAnnotation;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@FieldTypeAnnotation(tableName = "t_c_activity_mall_day_buy")
public class CrossActivityMallDayBuyData extends DataObject {
    @FieldTypeAnnotation(select = true, update = true, desc = "活动id")
    private int activityId;
    @FieldTypeAnnotation(update = true, desc = "活动id")
    private int mallId;
    @FieldTypeAnnotation(update = true, desc = "活动开始第几天")
    private int day;
    @FieldTypeAnnotation(desc = "购买累计")
    private long buyCount;
    @FieldTypeAnnotation(desc = "假进度")
    private long fakeBuyCount;
    @FieldTypeAnnotation(desc = "上次加假进度的时间秒")
    private long lastAddFakeTime;
    @FieldTypeAnnotation(desc = "区服购买累计")
    private Map<Long, Long> serverBuyCountMap = new HashMap<>();
    @FieldTypeAnnotation(desc = "待同步补发的区服")
    private List<Long> serverIdList = new ArrayList<>();
    @FieldTypeAnnotation(desc = "补发通知状态,默认0,1补发中,2补发完成")
    private int status;

    public CrossActivityMallDayBuyData() {
    }

    public CrossActivityMallDayBuyData(int activityId, int mallId, int day) {
        this.activityId = activityId;
        this.mallId = mallId;
        this.day = day;
        setInsertOption();
    }

    public int getActivityId() {
        return activityId;
    }

    public void setActivityId(int activityId) {
        this.activityId = activityId;
    }

    public int getMallId() {
        return mallId;
    }

    public void setMallId(int mallId) {
        this.mallId = mallId;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    public long getBuyCount() {
        return buyCount;
    }

    public void setBuyCount(long buyCount) {
        this.buyCount = buyCount;
        setUpdateOption();
    }

    public long getFakeBuyCount() {
        return fakeBuyCount;
    }

    public void setFakeBuyCount(long fakeBuyCount) {
        this.fakeBuyCount = fakeBuyCount;
        setUpdateOption();
    }

    public Map<Long, Long> getServerBuyCountMap() {
        return serverBuyCountMap;
    }

    public void setServerBuyCountMap(Map<Long, Long> serverBuyCountMap) {
        this.serverBuyCountMap = serverBuyCountMap;
        setUpdateOption();
    }

    public List<Long> getServerIdList() {
        return serverIdList;
    }

    public void setServerIdList(List<Long> serverIdList) {
        this.serverIdList = serverIdList;
        setUpdateOption();
    }

    public long getTotalBuyCount(){
        long count = this.buyCount + this.fakeBuyCount;
        return count;
    }

    public void setServerMallBuy(long serverId, long count) {
        long oldCount = serverBuyCountMap.getOrDefault(serverId, 0L);
        this.serverBuyCountMap.put(serverId, count);

        if (oldCount != count) {//数据有变动再算,不要一直算
            long totalCount = 0;
            for (Long serverCount : this.serverBuyCountMap.values()) {
                totalCount += serverCount;
            }
            setBuyCount(totalCount);
        }

        setUpdateOption();
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        if (this.status != status){
            this.status = status;
            setUpdateOption();
        }
    }

    public void removeServerId(long serverId){
        this.serverIdList.remove(serverId);
        setUpdateOption();
    }

    public long getLastAddFakeTime() {
        return lastAddFakeTime;
    }

    public void setLastAddFakeTime(long lastAddFakeTime) {
        this.lastAddFakeTime = lastAddFakeTime;
        setUpdateOption();
    }
}
